<template>
  <div class="dashboard-container" >
    <h1 style="text-align: center;margin-bottom: 40px;font-size: 35px;color: #486180">黑名单</h1>

    <el-dialog title="修改" :visible.sync="updateButtonController" width="30%" @close="closeBlackList('ruleForm')">
      <el-form :rules="roles" label-width="20%" :model="updateList" ref="ruleForm">
        <el-form-item label="失信程度" prop="state">
          <el-input v-model="updateList.state" maxlength="100" show-word-limit style="width: 80%" clearable placeholder="请输入失信程度(有轻度失信、中度失信、重度失信)"/>
        </el-form-item>
        <el-form-item label="信誉积分" prop="reputation">
          <el-input v-model="updateList.reputation" maxlength="100" show-word-limit style="width: 80%" clearable placeholder="请输入信誉积分"/>
        </el-form-item>

      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="info" plain @click="updateBlackList('ruleForm')">确 定</el-button>
        <el-button plain @click="closeBlackList('ruleForm')">取 消</el-button>
      </span>
    </el-dialog>


    <el-dialog title="删除" :visible.sync="deleteButtonController" width="30%" @close="closeDelete('ruleForm')">
      <div style="margin: 10px"><span>请确认是否拉出黑名单</span></div>
      <span slot="footer" class="dialog-footer">
          <el-button type="info" plain @click="deleteBlackList('ruleForm')">确定</el-button>
          <el-button plain @click="closeDelete('ruleForm')">取消</el-button>
      </span>
    </el-dialog>

    <el-input
      v-model="blackListId"
      placeholder="黑名单ID"
      name="blackListId"
      type="text"
      clearable
      style="margin-left:40px ;width: 180px"
    />
    <el-input
      v-model="customerName"
      placeholder="顾客名称"
      name="customerName"
      type="text"
      clearable
      style="margin-left:40px ;width: 180px"
    />
    <el-input
      v-model="state"
      placeholder="失信程度(有轻度失信、中度失信、重度失信)"
      name="state"
      type="text"
      clearable
      style="margin-left:40px ;width: 180px"
    />
    <el-button type="primary" plain style="width:80px;margin-bottom:30px;margin-left:40px;" @click="selectBlackList">查询</el-button>
    <el-button  style="width:80px;margin-bottom:30px;margin-left:40px;" @click="checkBlackList">重置</el-button>
    <el-table :data="blackList.slice((currentPage-1)*pagesize,currentPage*pagesize)" height="659px" border style="width: 100%;margin-top: 20px" class="elegant-room-table">
      <el-table-column align="center" fixed prop="blackListId" label="黑名单ID"/>
      <el-table-column align="center" fixed prop="customerId" label="顾客ID"/>
      <el-table-column align="center" fixed prop="customerName" label="顾客名称"/>
      <el-table-column align="center" fixed prop="state" label="失信程度"/>
      <el-table-column align="center" fixed prop="remark" label="备注"/>
      <el-table-column align="center" fixed prop="deleteIdentity" label="删除标识"/>
      <el-table-column align="center" fixed prop="debtMoney" label="欠款金额"/>
      <el-table-column align="center" fixed prop="debtDay" label="欠款天数"/>
      <el-table-column align="center" fixed prop="reputation" label="信誉积分"/>
      <el-table-column align="center" fixed label="操作">
        <template v-slot="scope">
          <!--  修改 -->
          <el-button class="el-icon-edit" circle @click="updateButton(scope.row)"></el-button>
          <el-button class="el-icon-delete" circle @click="deleteButton(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="margin-top: 10px; width: 100%; text-align: center;">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-sizes="[10,15,20]"
        :page-size="pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="blackList.length">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import { deleteCustomer, insertCustomer, selectAllCustomer, updateCustomer } from '@/api/Customer'
import { deleteBlackList, insertBlackList, selectAllBlackList, updateBlackList } from '@/api/blackList'
import { selectAllStay } from '@/api/stay'

export default {
  name: 'user',
  computed: {
    ...mapGetters([
      'userId',
      'userName',
      'roleName'
    ])
  },
  data(){
    return{
      blackListId:'',
      customerName:'',
      state:'',
      blackList:[],
      states:[],
      currentPage: 1,
      currentIndex: '',
      pagesize: 10,
      updateButtonController:false,
      deleteButtonController:false,
      deleteBlackListTableId:'',
      deleteBlackListId:'',
      updateList:{
        blackListTableId:'',
        blackListId:'',
        customerId:'',
        customerName:'',
        state:'',
        remark:'',
        debtMoney:'',
        debtDay:'',
        reputation:'',
      },
      roles: {
        customerName: [
          { required: true, trigger: 'blur', message: '用户名不能为空' },
          { min: 1, max: 11, message: '长度在1到11个数字', trigger: 'blur' }
        ],
        phoneNumber: [
          { required: true, trigger: 'blur', message: '手机号不能为空' },
          { min: 11, max: 11, message: '请输入11位手机号', trigger: 'blur' }
        ],
        identificationCard: [
          { required: true, trigger: 'blur', message: '身份证号不能为空' },
          { min: 18, max: 18, message: '请输入18位身份证号', trigger: 'blur' }
        ],
        sex: [
          { required: true, trigger: 'blur', message: '性别不能为空' },
        ],
      }
    }
  },
  mounted() {
    this.selectBlackList()
    this.fetchBlackList()
  },
  methods: {
    handleSizeChange(val) {
      this.pagesize = val;
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.currentPage = val
    },
    fetchBlackList() {
      selectAllBlackList().then(response => {
        this.states = Object.values(
          response.data.reduce((selectState, item) => {
            selectState[item.state] = item; // 保留最后一个出现的重复项
            return selectState;
          }, {})
        );
      });
    },
    selectBlackList() {
      this.currentPage = 1
      selectAllBlackList({
        blackListId: this.blackListId,
        customerName: this.customerName,
        state: this.state
      }).then(
        response => {
          console.log(response.data)
          this.blackList = response.data
        }
      )
    },
    updateButton(row) {
      this.updateList = {
        blackListTableId: row.blackListTableId,
        blackListId: row.blackListId,
        customerId: row.customerId,
        customerName: row.customerName,
        state: row.state,
        remark: row.remark,
        debtMoney: row.debtMoney,
        debtDay: row.debtDay,
        reputation: row.reputation,
      }
      this.updateButtonController = true
    },
    deleteButton(row) {
      this.deleteBlackListTableId = row.blackListTableId
      this.deleteBlackListId = row.blackListId
      this.deleteButtonController = true
    },
    updateBlackList(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          updateBlackList({
            blackListTableId: this.updateList.blackListTableId,
            blackListId: this.updateList.blackListId,
            customerId: this.updateList.customerId,
            state: this.updateList.state,
            remark: this.updateList.remark,
            debtMoney: this.updateList.debtMoney,
            debtDay: this.updateList.debtDay,
            reputation: this.updateList.reputation,
          }).then(
            response => {
              this.updateButtonController = false
              this.$message.success("修改成功！"); // 添加成功提示
              this.selectBlackList()
            }
          )
        } else {
          return false
        }
      })
    },
    checkBlackList() {
      this.blackListId = ''
      this.customerName = ''
      this.state = ''
      this.selectBlackList()
    },
    closeBlackList(formName) {
      this.$refs[formName].resetFields();
      this.updateButtonController = false;
    },

    closeDelete() {
      this.addButtonController = false
      this.updateButtonController = false
      this.deleteButtonController = false
    },
    deleteBlackList(formName) {
      deleteBlackList({
        blackListTableId: this.deleteBlackListTableId,
        blackListId: this.deleteBlackListId,
      }).then(
        async response => {
          this.deleteButtonController = false
          this.$message.success('已拉出黑名单');
          this.selectBlackList()
        })
    },

  }
}
</script>

<style lang="scss" scoped>
.dashboard {
  &-container {
    margin: 30px;
  }

  &-text {
    font-size: 30px;
    line-height: 46px;
  }
}
</style>
